package com.frame.work.common.util;

import org.apache.commons.lang3.StringUtils;

import java.time.DayOfWeek;
import java.time.LocalDateTime;


public class FrameMaker {


    /**
     * 获取网关场景触发命令
     */
    public static String getWriteGateway50SceneTrigger09Frame(String mac, int code) {
        String frame = "2A0D5000" + mac + "0209" + Util.intToHex(code);
        String checksum = Util.calculateHexChecksum(frame.substring(4));
        frame += checksum + "23";
        return frame.toUpperCase();
    }


    /**
     * 获取网关网关时间配置命令
     */
    public static String getWriteGatewayTimeFrame(String gatewayMac) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int monthValue = now.getMonthValue();
        int dayOfMonth = now.getDayOfMonth();
        int dayOfWeek = now.getDayOfWeek().getValue();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();
        String lsTime = new StringBuilder().append(Util.intToHex(Integer.parseInt((year + "").substring(2, 4))))
                .append(Util.intToHex(monthValue)).append(Util.intToHex(dayOfMonth)).append(Util.intToHex(dayOfWeek))
                .append(Util.intToHex(hour)).append(Util.intToHex(minute)).append(Util.intToHex(second))
                .toString();
        String frame = "0200" + gatewayMac + "13B00208" + lsTime + "00";
        String length = Integer.toHexString(frame.length() / 2);
        String checksum = Util.calculateHexChecksum(frame);
        frame = "2A" + length + frame + checksum + "23";
        return frame.toUpperCase();
    }

    /**
     * 网关心跳回应帧
     *
     * @return
     */
    public static String getWrite5020HeartResponseFrame() {
        return "2A0F02000000000000000000139C040101B723";
    }

    /**
     * 获取网关读设备状态命令
     */
    public static String getWriteGatewayReadDevice1007Frame(String gatewayMac, String deviceMac) {
        String frame = "0101" + gatewayMac + deviceMac + "03EF00";
        String length = Integer.toHexString(frame.length() / 2);
        String checksum = Util.calculateHexChecksum(frame);
        frame = "2A" + length + frame + checksum + "23";
        return StringUtils.upperCase(frame);
    }

    /**
     * 获取网关状态（布防、撤防、报警、非报警）信息
     */
    public static String getReadGateway50Frame(String mac) {
        String frame = "2A0E5000" + mac + "030300FF";
        String checksum = Util.calculateHexChecksum(frame.substring(4));
        frame += checksum + "23";
        return StringUtils.upperCase(frame);
    }
}
